package ui.popup
{
	import events.GCEvent;
	import ui.GCNumberScroller;
	import ui.button.GCButton;
	import ui.textfield.*;
	
	import flash.display.Bitmap;
	import flash.display.Shape;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.text.TextField;	

	/**
	 * ...
	 * @author Jason Won
	 */
	public class GCSellPopup extends Sprite
	{
		private static const RESOURCE_LABEL:String = "Name of Good:";
		private static const QUANTITY_LABEL:String = "Quantity to Sell:";
		private static const PRICE_PER_LABEL:String = "Price per unit to sell at:";
		private static const TOTAL_SALES_LABEL:String = "Profit if all sold:";
		
		/*
		Variables used to keep track of item information. Should be reset
		everytime this popup reappears
		*/
		private var _resourceName:String;
		private var _maxCanSell:uint;
		private var _pricePerUnit:int;
		private var _quantityToSell:int;
		private var _totalPrice:int;	
		
		/*
		Clickable components in the menu. 
		*/
		private var _quantityComponent:GCNumberScroller;
		private var _pricePerComponent:GCNumberScroller;
		private var _confirmButton:GCButton;
		private var _cancelButton:GCButton;
		private var _maxQuantityButton:GCButton;
		
		/*
		Various text labels
		*/
		private var _resourceLabel:GCTextField;
		private var _quantityLabel:GCTextField;
		private var _pricePerLabel:GCTextField;
		private var _totalPriceLabel:GCTextField;
		
		/*
		Labels to display values
		*/
		private var _resourceText:GCTextField;
		private var _totalPriceText:GCTextField;
		private var _totalCanSellText:GCTextField;
		
		/*
		 * Background 
		 */
		private var _backgroundImage:Sprite;
		
		/**
		 * Constructor
		 * @param	x
		 * @param	y
		 */
		public function GCSellPopup(x:int = 0, y:int = 0) 
		{
			this.x = x;
			this.y = y;
			
			_resourceName = "";
			_pricePerUnit = 0;
			_quantityToSell = 0;
			_totalPrice = 0;
			_maxCanSell = 0;
			
			_quantityComponent = new GCNumberScroller();
			_pricePerComponent = new GCNumberScroller();
			_confirmButton = new GCButton(onConfirmSell);
			_cancelButton = new GCButton(onCancelSell);
			_maxQuantityButton = new GCButton(onSetMaxQuantity);
			
			_resourceLabel = new GCTextField();
			_quantityLabel = new GCTextField();
			_pricePerLabel = new GCTextField();
			_totalPriceLabel = new GCTextField();
			
			_resourceText = new GCTextField();
			_totalPriceText = new GCTextField();
			_totalCanSellText = new GCTextField();
			
			this.init();
		}
		
		private function init():void
		{
			_quantityComponent.addEventListener(GCEvent.VALUE_CHANGED_EVENT, onValueChanged);
			_pricePerComponent.addEventListener(GCEvent.VALUE_CHANGED_EVENT, onValueChanged);
			
			_resourceLabel.text = RESOURCE_LABEL;
			_quantityLabel.text = QUANTITY_LABEL;
			_pricePerLabel.text = PRICE_PER_LABEL;
			_totalPriceLabel.text = TOTAL_SALES_LABEL;
			
			_resourceLabel.height = GCConstants.LABEL_HEIGHT;
			_resourceLabel.width = GCConstants.LABEL_WIDTH;
			_resourceLabel.x =  GCConstants.X_OFFSET;
			_resourceLabel.y =  GCConstants.Y_OFFSET;
			
			_quantityLabel.height = GCConstants.LABEL_HEIGHT;
			_quantityLabel.width = GCConstants.LABEL_WIDTH;
			_quantityLabel.y = _resourceLabel.y + _resourceLabel.height;
			_quantityLabel.x =  GCConstants.X_OFFSET;
			
			_pricePerLabel.height = GCConstants.LABEL_HEIGHT;
			_pricePerLabel.width = GCConstants.LABEL_WIDTH;
			_pricePerLabel.y = _quantityLabel.y + _quantityLabel.height;
			_pricePerLabel.x =  GCConstants.X_OFFSET;
			
			_totalPriceLabel.height = GCConstants.LABEL_HEIGHT;
			_totalPriceLabel.y = _pricePerLabel.y + _pricePerLabel.height;
			_totalPriceLabel.width = GCConstants.LABEL_WIDTH;
			_totalPriceLabel.x =  GCConstants.X_OFFSET;
			
			_resourceText.text = _resourceName;
			_resourceText.width = GCConstants.LABEL_WIDTH;
			_resourceText.height = GCConstants.LABEL_HEIGHT;
			_resourceText.x = _resourceLabel.x + _resourceLabel.width;
			_resourceText.y = _resourceLabel.y;
			
			_quantityComponent.x = _quantityLabel.x + _quantityLabel.width;
			_quantityComponent.y = _quantityLabel.y;
			_totalCanSellText.x = _quantityComponent.x + _quantityComponent.width;
			_totalCanSellText.y = _quantityComponent.y;
			
			_pricePerComponent.x = _pricePerLabel.x + _pricePerLabel.width;
			_pricePerComponent.y = _pricePerLabel.y;
			
			_totalPriceText.text = String(_totalPrice);
			_totalPriceText.width = 50;
			_totalPriceText.height = GCConstants.LABEL_HEIGHT;
			_totalPriceText.x = _totalPriceLabel.x + _totalPriceLabel.width;
			_totalPriceText.y = _totalPriceLabel.y;
			
			/*
			The buttons 
			*/
			_confirmButton.loadGraphic(GCResources.plate, 100, 30);
			_confirmButton.loadText("OK");
			_confirmButton.y = _totalPriceLabel.y + GCConstants.LABEL_HEIGHT;
			
			_cancelButton.loadGraphic(GCResources.plate, 100, 30);
			_cancelButton.loadText("Cancel");
			_cancelButton.y = _confirmButton.y;
			_cancelButton.x = _confirmButton.x + _confirmButton.width;
			
			_maxQuantityButton.loadGraphic(GCResources.plate, 100, 30);
			_maxQuantityButton.loadText("Sell All");
			_maxQuantityButton.x = _totalCanSellText.x;
			_maxQuantityButton.y = _totalCanSellText.y + _totalCanSellText.height / 2;
			
			this.addChild(_quantityComponent);
			this.addChild(_pricePerComponent);
			this.addChild(_confirmButton);
			this.addChild(_cancelButton);
			
			this.addChild(_resourceLabel);
			this.addChild(_quantityLabel);
			this.addChild(_pricePerLabel);
			this.addChild(_totalPriceLabel);
			
			this.addChild(_resourceText);
			this.addChild(_totalPriceText);
			this.addChild(_totalCanSellText);
			this.addChild(_maxQuantityButton);
			
			//Popup background
			_backgroundImage = GCResources.getMaskedCanvasBackground(this.width, this.height);
			this.addChildAt(_backgroundImage, 0);
		}		
		
		//----------------------------------------------------------------------
		//	PUBLIC FUNCTIONS
		//----------------------------------------------------------------------
		
		/**
		 * Initializes the values for the popup
		 * @param	maxQuantity
		 * @param	resourceName
		 */
		public function initializePopup(maxQuantity:int, resourceName:String):void
		{
			_maxCanSell = maxQuantity;
			_quantityComponent.setLimit(_maxCanSell);
			_resourceName = resourceName;
			
			_totalCanSellText.text = "Max amount can sell: " + String(_maxCanSell);
			_resourceText.text = resourceName;
			
			// Enable the buttons
			_cancelButton.enable();
			_confirmButton.enable();
			_maxQuantityButton.enable();
		}
		
		/**
		 * Clears the popup to default
		 */
		public function clearPopup():void
		{
			_resourceName = "";
			_quantityComponent.setValue(0);
			_pricePerComponent.setValue(0);
			_quantityToSell = 0;
			_pricePerUnit = 0;
			_totalPrice = 0;
		}		
		
		//----------------------------------------------------------------------
		//	PRIVATE FUNCTIONS
		//----------------------------------------------------------------------
		
		/**
		 * Cleans up the popup before closing
		 */
		private function onExitCleanup():void
		{
			// Disable the buttons
			_cancelButton.disable();
			_confirmButton.disable();
			_maxQuantityButton.disable();
		}
		
		//----------------------------------------------------------------------
		//	EVENT HANDLERS
		//----------------------------------------------------------------------
		
		/**
		 * Handles setting the quantity on the number scroller to max
		 * @param	event
		 */
		private function onSetMaxQuantity(event:MouseEvent):void
		{
			_quantityComponent.setValue(_maxCanSell);
		}
		
		/**
		 * Handles event for changing values
		 * @param	event
		 */
		private function onValueChanged(event:Event):void
		{
			_quantityToSell = _quantityComponent.value;
			_pricePerUnit = _pricePerComponent.value;
			_totalPrice = _quantityToSell * _pricePerUnit;
			
			_totalPriceText.text = String(_totalPrice);
		}
		
		/**
		 * Handles events for confimation of a sell
		 * @param	event
		 */
		private function onConfirmSell(event:MouseEvent):void
		{
			// Cleans up the popup before closing
			onExitCleanup();
			
			if (_quantityToSell > 0)
			{
				dispatchEvent(new Event(GCEvent.SELL_CONFIRM_EVENT));
			}
			else
			{
				dispatchEvent(new Event(GCEvent.CANCEL_EVENT));
			}
		}
		
		/**
		 * Handles the event for canceling a sell
		 * @param	event
		 */
		private function onCancelSell(event:MouseEvent):void
		{
			// Cleans up the popup before closing
			onExitCleanup();
			
			dispatchEvent(new Event(GCEvent.CANCEL_EVENT));
		}
		
		//----------------------------------------------------------------------
		//	GET FUNCTIONS
		//----------------------------------------------------------------------		
		
		public function get pricePerUnit():int
		{
			return _pricePerUnit;
		}
		
		public function get quantityToSell():int
		{
			return _quantityToSell;
		}
		
	}
}